'''
链表合并：
使用迭代的方式，确保列表顺序，然后根据大小比对节点，插入链表，然后删除链表。
最后需要注意把剩下的链表插入。
'''
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode:
    dummy = ListNode()
    current = dummy

    # 循环把节点挂在到current上
    while l1 and l2:
        if l1.val < l2.val:
            current.next = l1
            l1 = l1.next
        else:
            current.next = l2
            l2 = l2.next
        current = current.next

    # 把最后剩下的节点挂在
    if l1:
        current.next = l1
    else:
        current.next = l2

    return dummy.next

def printList(node: ListNode):
    while node:
        print(node.val, end=" -> ")
        node = node.next
    print("None")

# Helper function to create a linked list from a list of values
def createList(values):
    if not values:
        return None
    head = ListNode(values[0])
    current = head
    for value in values[1:]:
        current.next = ListNode(value)
        current = current.next
    return head

# Test cases
def test_mergeTwoLists():
    # Test case 1
    l1 = createList([1, 2, 4])
    l2 = createList([1, 3, 4])
    merged = mergeTwoLists(l1, l2)
    print("Test case 1:")
    printList(merged)  # Expected output: 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> None

    # Test case 2
    l1 = createList([])
    l2 = createList([0])
    merged = mergeTwoLists(l1, l2)
    print("Test case 2:")
    printList(merged)  # Expected output: 0 -> None

    # Test case 3
    l1 = createList([1])
    l2 = createList([1])
    merged = mergeTwoLists(l1, l2)
    print("Test case 3:")
    printList(merged)  # Expected output: 1 -> 1 -> None

    # Test case 4
    l1 = createList([1, 3, 5])
    l2 = createList([2, 4, 6])
    merged = mergeTwoLists(l1, l2)
    print("Test case 4:")
    printList(merged)  # Expected output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> None

    # Test case 5
    l1 = createList([-1, 0, 1])
    l2 = createList([-2, 2])
    merged = mergeTwoLists(l1, l2)
    print("Test case 5:")
    printList(merged)  # Expected output: -2 -> -1 -> 0 -> 1 -> 2 -> None

# Run the test cases
test_mergeTwoLists()